 <?php
//--------------------------------------------------------------------------------------
//=========================================script traiter par MEZOUARI KHALID===========
//--------------------------------------------------------------------------------------
require_once('config.php');
require_once('ConnectBD.php');
require_once('dao/tacheDao.php');
require_once('dao/nomadeDao.php');
require_once('dao/compteRenduDao.php');
require_once('dao/interventionDao.php');
require_once('beans/compteRenduBean.php');
require_once('dao/materielCommandeDao.php');

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
extract($_GET);
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');

/** Include PHPExcel */
require_once 'PHPExcel/Classes/PHPExcel.php';


// Create new PHPExcel object
echo date('H:i:s') , " Create new PHPExcel object" , EOL;
$objPHPExcel = new PHPExcel();

// Set document properties
echo date('H:i:s') , " Set document properties" , EOL;
$objPHPExcel->getProperties()->setCreator("Mezouari")
				     ->setLastModifiedBy("Mezouari")
				     ->setTitle("Liste des documents")
				     ->setSubject("mediaFast")
				     ->setDescription("mediaFast pour la gestion des nomades")
				     ->setKeywords("technicien nomade")
				     ->setCategory("file");



  // Le contenu du pdf ou le body
 //--------------------------------------------
 //*********************************compterendu
 //--------------------------------------------
    $nomFichier="";//nom du fichier 
  if($page=="compteRendu"){
    echo date('H:i:s') , " Add some data" , EOL;
    $compteRenduDao=new compteRenduDao();                    //instancier l'objet compteRenduDao
    $compteRendus = $compteRenduDao->loadAllComptesRendus();//importer liste des donnees
    // entete du tableau on va colorer les elements de l'entete
    $objPHPExcel->getActiveSheet()->setCellValue("C1","Liste des comptes rendus");
    $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_GREEN);
    $objPHPExcel->getActiveSheet()->getStyle('A2')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
    $objPHPExcel->getActiveSheet()->getStyle('B2')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
    $objPHPExcel->getActiveSheet()->getStyle('C2')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
    $objPHPExcel->getActiveSheet()->getStyle('D2')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
    $objPHPExcel->setActiveSheetIndex(0)
            //definir les elements de l'entete
            ->setCellValue('A2', 'ID')
            ->setCellValue('B2', 'DESIGNATION')
            ->setCellValue('C2', 'DESCRIPTION')
            ->setCellValue('D2', 'DATE FIN DE TACHE');
            $ligne=3;//les enregistrements vont debuter appartir de la ligne 3 du fichier excel
  foreach ($compteRendus as $compteRendu) {
    $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(300);//modifier width de la colonne B
    $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(600);//modifier width de la colonne C		
    //remplir les lignes de la feuille xcl
    $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue("A$ligne", $compteRendu->getIdCompteRendu())
            ->setCellValue("B$ligne", $compteRendu->getDesignation())
            ->setCellValue("C$ligne", $compteRendu->getDescription())
            ->setCellValue("D$ligne", $compteRendu->getDateFin());
            $ligne=$ligne+1;
   }
   $nomFichier="compteRendu";//associer un nom au fichier
}//fin compte rendu
//--------------------------------------------
//**************************************NOMADE
//--------------------------------------------
  if($page=="nomade"){
        echo date('H:i:s') , " Add some data" , EOL;
        $nomadeDao=new nomadeDao(); 
        $nomades = $nomadeDao->loadAllNomade();
       // entete du tableau on va colorer les elements de l'entete
        $objPHPExcel->getActiveSheet()->setCellValue("C1","Liste des NOMADES");
        $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_GREEN);
        $objPHPExcel->getActiveSheet()->getStyle('A2')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
        $objPHPExcel->getActiveSheet()->getStyle('B2')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
        $objPHPExcel->getActiveSheet()->getStyle('C2')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
        $objPHPExcel->getActiveSheet()->getStyle('D2')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
        $objPHPExcel->getActiveSheet()->getStyle('E2')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
        $objPHPExcel->getActiveSheet()->getStyle('F2')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
        //definir les elements de l'entete
        $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('A2', 'ID')
                    ->setCellValue('B2', 'CIN')
                    ->setCellValue('C2', 'NOM')
                    ->setCellValue('D2', 'PRENOM')
                    ->setCellValue('E2', 'DATE NAISSANCE')
                    ->setCellValue('F2', 'FONCTION');
            $ligne=3;//les enregistrements vont debuter appartir de la ligne 3 du fichier excel
     foreach ($nomades as $nomade) {
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(50);
	  $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(50);
	  $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(50);
        $objPHPExcel->getActiveSheet()->getStyle('B')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_DARKRED);
        //remplir les lignes de la feuille xcl
        $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue("A$ligne", $nomade->getIdnomade())
                    ->setCellValue("B$ligne", $nomade->getCIN())
                    ->setCellValue("C$ligne", $nomade->getNom())
                    ->setCellValue("D$ligne", $nomade->getPrenom())
                    ->setCellValue("E$ligne", $nomade->getDateNaissance())
                    ->setCellValue("F$ligne", $nomade->getFonction());
            $ligne=$ligne+1;
     }
$nomFichier="Nomade";
}
//--------------------------------------------
//************************************PLANNING
//--------------------------------------------
 if($page=="planning"){
       echo date('H:i:s') , " Add some data" , EOL;
       $tacheDao=new tacheDao(); 
       $taches = $tacheDao->loadAllTache();
      // entete du tableau on va colorer les elements de l'entete
       $objPHPExcel->getActiveSheet()->setCellValue("C1","Liste des NOMADES");
       $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_GREEN);
       $objPHPExcel->getActiveSheet()->getStyle('A2')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
       $objPHPExcel->getActiveSheet()->getStyle('B2')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
       $objPHPExcel->getActiveSheet()->getStyle('C2')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
       $objPHPExcel->getActiveSheet()->getStyle('D2')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
       //definir les elements de l'entete
       $objPHPExcel->setActiveSheetIndex(0)
                   ->setCellValue('A2', 'ID')
                   ->setCellValue('B2', 'OBJECTIF')
                   ->setCellValue('C2', 'DESIGNATION')
                   ->setCellValue('D2', 'DATE DEBUT');
            $ligne=3;//les enregistrements vont debuter appartir de la ligne 3 du fichier excel
     foreach ($taches as $tache) {
         $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(50);
	   $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(50);
	   $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(50);
         $objPHPExcel->getActiveSheet()->getStyle('B')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_DARKRED);
	   //remplir les lignes de la feuille xcl
         $objPHPExcel->setActiveSheetIndex(0)
                     ->setCellValue("A$ligne", $tache->getIdTache())
                     ->setCellValue("B$ligne", $tache->getObjectif())
                     ->setCellValue("C$ligne", $tache->getDesignation())
                     ->setCellValue("D$ligne", $tache->getDateDebut());
            $ligne=$ligne+1;
   }
   $nomFichier="Planning";
}
//--------------------------------------------
//********************************INTERVENTION
//--------------------------------------------
if($page=="intervention"){
      echo date('H:i:s') , " Add some data" , EOL;
      $interventionDao=new interventionDao();                   //instancier l objet tacheDao
      $interventions = $interventionDao->loadAllIntervention();// importer la liste des donnees
      // entete du tableau on va colorer les elements de l'entete
      $objPHPExcel->getActiveSheet()->setCellValue("C1","Liste des interventions");
      $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_GREEN);
      $objPHPExcel->getActiveSheet()->getStyle('A2')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
      $objPHPExcel->getActiveSheet()->getStyle('B2')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
      $objPHPExcel->getActiveSheet()->getStyle('C2')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
      $objPHPExcel->getActiveSheet()->getStyle('D2')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
      //definir les elements de l'entete
      $objPHPExcel->setActiveSheetIndex(0)
                  ->setCellValue('A2', 'ID')
                  ->setCellValue('B2', 'OBJECTIF')
                  ->setCellValue('C2', 'DESIGNATION')
                  ->setCellValue('D2', 'DATE DEBUT')
                  ->setCellValue('E2', 'Nomade Cible');
                  $ligne=3;//les enregistrements vont debuter appartir de la ligne 3 du fichier excel
     foreach ($interventions as $intervention) {
         $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(50);
	   $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(50);
	   $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(50);
         $objPHPExcel->getActiveSheet()->getStyle('B')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_DARKRED);	
          //remplir les lignes de la feuille xcl
         $objPHPExcel->setActiveSheetIndex(0)
                     ->setCellValue("A$ligne", $intervention->getIdNomade())
                     ->setCellValue("B$ligne", $intervention->getObjectif())
                     ->setCellValue("C$ligne", $intervention->getDesignation())
                     ->setCellValue("D$ligne", $intervention->getDateDebut())
                     ->setCellValue("E$ligne", $intervention->getIdNomadeCible());
                     $ligne=$ligne+1;
      }
      $nomFichier="Intervention";
}
//--------------------------------------------
//***************************MATERIEL COMMANDE
//--------------------------------------------
 if($page=="materielCommande"){
       echo date('H:i:s') , " Add some data" , EOL;
       $materielCommandeDao=new materielCommandeDao();                       //instancier l objet materielCommandeDao
       $materielCommandes = $materielCommandeDao->loadAllMaterielCommande();// importer la liste des donnees
      // entete du tableau on va colorer les elements de l'entete
       $objPHPExcel->getActiveSheet()->setCellValue("C1","Liste des MATERIELS");
       $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_GREEN);
       $objPHPExcel->getActiveSheet()->getStyle('A2')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
       $objPHPExcel->getActiveSheet()->getStyle('B2')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
       $objPHPExcel->getActiveSheet()->getStyle('C2')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
       $objPHPExcel->getActiveSheet()->getStyle('D2')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
       $objPHPExcel->getActiveSheet()->getStyle('E2')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
       $objPHPExcel->getActiveSheet()->getStyle('F2')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
       $objPHPExcel->getActiveSheet()->getStyle('G2')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
       //definir les elements de l'entete
       $objPHPExcel->setActiveSheetIndex(0)
                   ->setCellValue('A2', 'REFERENCE')
                   ->setCellValue('B2', 'CONSTRUCTEUR')
                   ->setCellValue('C2', 'CONNECTEUR')
                   ->setCellValue('D2', 'TYPE')
                   ->setCellValue('E2', 'QUANTITE')
                   ->setCellValue('F2', 'DATE COMMANDE')
                   ->setCellValue('G2', 'NOMADE CIBLE');
                   $ligne=3;//les enregistrements vont debuter appartir de la ligne 3 du fichier excel
      foreach ($materielCommandes as $materielCommande) {      
         $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
	   $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
	   $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
         $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(30);
         $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(50);
         $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
         $objPHPExcel->getActiveSheet()->getStyle('B')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_DARKRED);		
         //remplir les lignes de la feuille xcl
         $objPHPExcel->setActiveSheetIndex(0)
                     ->setCellValue("A$ligne", $materielCommande->getReference())
                     ->setCellValue("B$ligne", $materielCommande->getConstructeur())
                     ->setCellValue("C$ligne", $materielCommande->getConnecteur())
                     ->setCellValue("D$ligne", $materielCommande->getType())
                     ->setCellValue("E$ligne", $materielCommande->getQuantite())
                     ->setCellValue("F$ligne", $materielCommande->getDateCommande())
                     ->setCellValue("G$ligne", $materielCommande->getIdNomade());
                     $ligne=$ligne+1;
    }
    $nomFichier="materielCommande";
}
//--------------------------------------------
//fin traitement des resultat
//--------------------------------------------

 // Rename worksheet
echo date('H:i:s') , " Rename worksheet" , EOL;
$objPHPExcel->getActiveSheet()->setTitle('MediaFast');

// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);

// Save Excel 95 file
echo date('H:i:s') , " Write to Excel5 format" , EOL;
$callStartTime = microtime(true);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save(str_replace('.php',$nomFichier.md5(uniqid(rand(), true)).'.xls', __FILE__));
$callEndTime = microtime(true);
$callTime = $callEndTime - $callStartTime;

echo date('H:i:s') , " File written to " , str_replace('.php', '.xls', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
echo 'Call time to write Workbook was ' , sprintf('%.4f',$callTime) , " seconds" , EOL;
// Echo memory usage
echo date('H:i:s') , ' Current memory usage: ' , (memory_get_usage(true) / 1024 / 1024) , " MB" , EOL;


// Echo memory peak usage
echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL;

// Echo done
echo date('H:i:s') , " Done writing files" , EOL;
echo 'Files have been created in ' , getcwd() , EOL;

?>